#ifndef __TCP_H__
#define __TCP_H__


#include "unix/unix.h"
#include "uv_common.h"

#ifdef __cplusplus
extern "C" {
#endif
typedef struct _tcp tcp_t;

typedef void (*uv_connection_cb)(tcp_t* h, int status);

struct _tcp{
    SOCKET socket;

    int addrlen;
    struct sockaddr *addr; //malloc and free internal

    ReqCommon_t *listen_req; //handled internal
    ReqCommon_t *connect_req; //handled internal
    RecvReq_t *recv_req; //handled internal

    SendReq_t *send_req; //handled outside
    ReqCommon_t *shutdown_req; //handled outside
    ReqCommon_t *close_req; //handled outside

    uv_connection_cb on_connect_cb; //客户端连接　服务端接受连接
    uv_recv_cb recv_cb;
    uv_send_cb send_cb;
    uv_close_cb close_cb;
    uv_shutdown_cb shutdown_cb;

    void *data; //handled outside
};

int tcp_init();
int tcp_createhandler(tcp_t *tcphandle, struct sockaddr *addr, size_t addrlen);
int tcp_listen(tcp_t* handle, int backlog, uv_connection_cb cb);
int tcp_accept(tcp_t* server, tcp_t* client);
void tcp_connect(tcp_t *client, const struct sockaddr* addr, size_t addrlen, uv_connection_cb cb);

void tcp_recv_start(tcp_t *tcphandle, uv_recv_cb cb);

void tcp_send(SendReq_t *sendreq, tcp_t *handle, const char *buf, size_t len, uv_send_cb cb);
void tcp_shutdown(ReqCommon_t *req, tcp_t *tcphandle,uv_shutdown_cb cb);
void tcp_close(ReqCommon_t* req, tcp_t* tcphandle, uv_close_cb cb);
int tcp_getsockname(const tcp_t* handle, struct sockaddr* name, int* namelen);


#ifdef __cplusplus
}
#endif

#endif // !__TCP_H__
